//
//  JavaScriptCoreViewController.m
//  OCUI
//
//  Created by login on 15-7-10.
//
//


#import "JavaScriptCoreViewController.h"
#import <objc/runtime.h>
#import <WebKit/WebKit.h>
@interface JavaScriptCoreViewController ()
{
    UITextField *textField;
}

@end

@implementation JavaScriptCoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    JSContext *context = [[JSContext alloc]init];
    JSValue *result = [context evaluateScript:@"2 + 2"];
    NSLog(@"2 + 2 = %d", [result toInt32]);
    
    [self factorial];
    
    JSContext *context1 = [[JSContext alloc] init];
    [context evaluateScript:@"var arr = [21, 7 , 'iderzheng.com'];"];
    JSValue *jsArr = context1[@"arr"]; // Get array from JSContext
    
    NSLog(@"JS Array: %@; Length: %@", jsArr, jsArr[@"length"]);
    jsArr[1] = @"blog"; // Use JSValue as array
    jsArr[7] = @7;
    
    NSLog(@"JS Array: %@; Length: %d", jsArr, [jsArr[@"length"] toInt32]);
    
    NSArray *nsArr = [jsArr toArray];
    NSLog(@"NSArray: %@", nsArr);
    
    
    
    JSContext *context11 = [[JSContext alloc] init];
    [context11 evaluateScript:@"function add(a, b) { return a + b; }"];
    JSValue *add = context11[@"add"];
    NSLog(@"Func: %@", add);
    JSValue *sum = [add callWithArguments:@[@(7), @(21)]];
    NSLog(@"Sum: %d",[sum toInt32]);
    
    
    JSContext *ooo = [[JSContext alloc]init];
    [ooo evaluateScript:@"function addd(a,b) { return a+b+10; }"];
    JSValue *addd = ooo[@"addd"];
    NSLog(@"%@ ",addd);
    JSValue *uuu = [addd callWithArguments:@[@(4),@(12)]];
    NSLog(@"%d",[uuu toInt32]);
    
    
#pragma mark - JSExport protocol
    
    textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 100, 50, 30)];
    textField.text = @"7";
    textField.textColor = [UIColor blackColor];
    class_addProtocol([UITextField class], @protocol(JSUITextFieldExport));
    
    [self.view addSubview:textField];
    
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 150, 50, 30)];
    [button setBackgroundColor:[UIColor redColor]];
    [button addTarget:self action:@selector(pressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}
- (void)pressed
{
    JSContext *context = [[JSContext alloc] init];
    
    context[@"textField"] = textField;
    
    NSString *script = @"var num = parseInt(textField.text, 10);"
    "++num;"
    "textField.text = num;";
    [context evaluateScript:script];
}

- (void) factorial
{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"text"ofType:@"js"];
    NSString *testScript = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];//文件编码
    
    JSContext *context = [[JSContext alloc]init];
    [context evaluateScript:testScript];//从oc到js
    
    JSValue *function = context[@"factorial"];//方法名
    JSValue *result = [function callWithArguments:@[@10]];//调用方法传入参数
    NSLog(@"factorial(10) = %d", [result toInt32]);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
